﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
using System.Text;
using dotless.Core.configuration;
using dotless.Core;
using Twipler.App.DataDoc;
using Twipler.Data.Entity.Theme;

namespace Twipler.App.Render
{
    public class StylesheetProducer 
    {
        private int _styleId;
        public string HttpPath;

        public StylesheetProducer()            
        {
            HttpPath = "settings/static/blank.css";
        }

        public override string ToString()
        {
            return "StyleSheetProducer[" + HttpPath + "]";
        }

        public static void RenderFor(StringBuilder html, IDataDoc user)
        {
            html.Append(Render(CreateLessHeaderForVisitor(user, Current.Visitor.Style)));
        }
       
        private static string Render(string less)
        {
            DotlessConfiguration config = DotlessConfiguration.Default;
            config.MinifyOutput = false;

            ILessEngine engine = (new EngineFactory()).GetEngine(config);
            LessSourceObject ob = new LessSourceObject() { Content = less };
            
            return engine.TransformToCss(ob);
        }

        private void CreateTempFile()
        {
            PublishTempFile tempFile = new PublishTempFile(".css");
            tempFile.Write(Render(CreateLessHeaderForVisitor(Current.Visitor.Details, Current.Visitor.Style)));
            
            _styleId = Current.Visitor.Style.Id;
            HttpPath = tempFile.HttpPath;

            Current.SessionTrack.AddToCleanUp(tempFile);
        }

        private static string CreateLessHeaderForVisitor(IDataDoc user, Style style)
        {
            StringBuilder lss = new StringBuilder();
            
            lss.AppendFormat("@profile_background_image_url: url({0}); ", user.Find("/profile_background_image_url").Value);
            lss.AppendFormat("@profile_link_color: #{0}; ", user.Find("/profile_link_color").Value);
            lss.AppendFormat("@profile_sidebar_fill_color: #{0}; ", user.Find("/profile_sidebar_fill_color").Value);
            lss.AppendFormat("@profile_image_url: url({0}); ", user.Find("/profile_image_url").Value);
            lss.AppendFormat("@profile_sidebar_border_color: #{0}; ", user.Find("/profile_sidebar_border_color").Value);
            lss.AppendFormat("@profile_background_color: #{0}; ", user.Find("/profile_background_color").Value);
            lss.AppendFormat("@profile_text_color: #{0}; ", user.Find("/profile_text_color").Value);

            lss.Append(style.Content);

            return lss.ToString();
        }

        internal void ReactToChange()
        {
            // No style, or style we've already rendered
            if ((Current.Visitor.Style == null) || (Current.Visitor.Style.Id == _styleId))
                return;

            // Write a new stylesheet
            CreateTempFile();
        }
    }
}
